package com.fenghuajueli.module_user.fragment;

import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants;
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog;
import com.fenghuajueli.libbasecoreui.feedback.FeedbackPrevActivity;
import com.fenghuajueli.libbasecoreui.feedback.ReportFeedbackActivity;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;
import com.fenghuajueli.libbasecoreui.mvp.OtherBaseFragment;
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.utils.JumpActivityUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.module_route.PermissionCenterModuleRoute;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.databinding.FragmentUserMineBinding;
import com.fenghuajueli.module_user.model.RequestInfoModel;
import com.sy.module_permission_center_hmy.ThirdManifestActivity;
import com.sy.module_permission_center_hmy.collectlist.PersonInfoCollectionListActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 我的页面，弄成fragment方便使用
 */
@Deprecated
@Route(path = UserModuleRoute.FRAGMENT_USER_MINE_SEANCE)
public class UserMineFragment extends OtherBaseFragment implements View.OnClickListener {
    private CommonTipsDialog commonTipsDialog;
    FragmentUserMineBinding binding;

    //本地存储头像标识
    private final String LOCAL_AVATAR_KIND = "local_avatar_kind";

    private static final String KEY_RED_DOT_CLICKED = "key_red_dot_clicked";
    private static final String KEY_LAST_VERSION_CODE = "key_last_app_version_code";

    @Override
    public void initView(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        binding = FragmentUserMineBinding.bind(getContentView());
        binding.myActionBar.setRightIconClick(new OnBaseClick<Integer>() {
            @Override
            public void onClick(Integer integer) {
                ARouter.getInstance().build(UserModuleRoute.USER_SETTING_ACTIVITY).navigation();
            }
        });
        binding.userInfoContainer.setOnClickListener(this);
        binding.stVipContainer.setOnClickListener(this);
        binding.stToComment.setOnClickListener(this);
        binding.stUserFeedBack.setOnClickListener(this);
        binding.stClearCache.setOnClickListener(this);
        binding.stAboutUs.setOnClickListener(this);
        binding.stContactServer.setOnClickListener(this);
        binding.stAllAgreement.setOnClickListener(this);
        binding.stReportFeedBack.setOnClickListener(this);
        binding.stInfoCollectList.setOnClickListener(this);
        binding.stThirdSdkList.setOnClickListener(this);
        initUserInfo();
        changVipShowStatus();
        updateViewsContent();
        redDotState();

        if (!AppConfigInfo.ICP.isEmpty()){
            binding.tvICP.setVisibility(View.VISIBLE);
            binding.tvICP.setText(AppConfigInfo.ICP);
        }
    }

    private void redDotState(){
        int lastVersionCode = MmkvUtils.get(KEY_LAST_VERSION_CODE, -1);
        boolean clicked = MmkvUtils.get(KEY_RED_DOT_CLICKED,false);
        if (AppConfigInfo.VERSION_CODE  > lastVersionCode) {
            binding.vRedDot.setVisibility(View.VISIBLE);
            MmkvUtils.save(KEY_LAST_VERSION_CODE,AppConfigInfo.VERSION_CODE);
            MmkvUtils.save(KEY_RED_DOT_CLICKED,false);
        } else if (!clicked) {
            binding.vRedDot.setVisibility(View.VISIBLE);
        }else {
            binding.vRedDot.setVisibility(View.GONE);
        }
    }

    /**
     * 根据开关设置隐藏vip相关信息
     */
    private void changVipShowStatus() {
        if (SwitchKeyUtils.getPayStatus()) {
            binding.stVipContainer.setVisibility(View.VISIBLE);
            binding.ivUserVipStatus.setVisibility(View.VISIBLE);
            binding.userInfoContainer.setVisibility(View.VISIBLE);
        } else {
            binding.stVipContainer.setVisibility(View.GONE);
            binding.ivUserVipStatus.setVisibility(View.GONE);
            binding.userInfoContainer.setVisibility(View.GONE);
        }
    }

    private void updateViewsContent() {
        //head
        String imgUrl = MmkvUtils.get(LOCAL_AVATAR_KIND, "");
        if (imgUrl.isEmpty() || !UserInfoUtils.getInstance().isLogin() &&
                MmkvUtils.get(RequestInfoModel.LOGIN_OUT, false)) {
            Glide.with(this).load(R.mipmap.icon_default_avatar).into(binding.cvUserHead);
        } else {
            Glide.with(this).load(imgUrl).into(binding.cvUserHead);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        changVipShowStatus();
        updateViewsContent();
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.userInfoContainer) {
            if (UserInfoUtils.getInstance().isLogin() || !MmkvUtils.get(RequestInfoModel.LOGIN_OUT, false)) {
                ARouter.getInstance().build(UserModuleRoute.USER_INFO_ACTIVITY).navigation();
            } else {
                ARouter.getInstance().build(UserModuleRoute.USER_LOGIN).navigation();
            }
        } else if (view.getId() == R.id.stVipContainer) {
            ARouter.getInstance().build(UserModuleRoute.USER_OPEN_VIP_ACTIVITY).navigation();
        } else if (view.getId() == R.id.stToComment) {
            PublicFunction.goComment(getContext());
        } else if (view.getId() == R.id.stUserFeedBack) {
            MmkvUtils.save(KEY_RED_DOT_CLICKED,true);
            binding.vRedDot.setVisibility(View.GONE);
            JumpActivityUtils.goNormalActivity(getContext(), FeedbackPrevActivity.class);
//            if (SwitchKeyUtils.getFeedbackType() == 1) {
//                JumpActivityUtils.goNormalActivity(getContext(), OneFeedbackActivity.class);
//            } else if (SwitchKeyUtils.getFeedbackType() == 2) {
//                JumpActivityUtils.goNormalActivity(getContext(), TwoFeedbackActivity.class);
//            }
        } else if (view.getId() == R.id.stClearCache) {
            showCommTipDialog();
        } else if (view.getId() == R.id.stAboutUs) {
            JumpActivityUtils.goAboutUsActivity(getContext());
        } else if (view.getId() == R.id.stContactServer) {
            CustomerServiceActivity.start(getContext());
        } else if (view.getId() == R.id.stAllAgreement) {
            ARouter.getInstance().build(PermissionCenterModuleRoute.PRIVACY_SETTING_ACTIVITY).navigation();
        } else if (view.getId() == R.id.stReportFeedBack) {
            JumpActivityUtils.goNormalActivity(getContext(), ReportFeedbackActivity.class);
        }else if (view.getId() == R.id.stInfoCollectList) {
            PersonInfoCollectionListActivity.start(requireContext());
        }else if (view.getId() == R.id.stThirdSdkList) {
            ThirdManifestActivity.start(requireContext());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(EventEntity eventBusEntity) {
        if (eventBusEntity.getCode() == EventBusConstants.REGISTER_SUCCESS) {
            initUserInfo();
        }
        if (eventBusEntity.getCode() == EventBusConstants.LOGIN_SUCCESS) {
            initUserInfo();
        }
        if (eventBusEntity.getCode() == EventBusConstants.LOGIN_OUT) {
            initUserInfo();
        }
        if (eventBusEntity.getCode() == EventBusConstants.DELETE_USER) {
            initUserInfo();
        }
        if (eventBusEntity.getCode() == EventBusConstants.UPDATE_USER_INFO) {
            initUserInfo();
        }
    }

    private void initUserInfo() {
        if (UserInfoUtils.getInstance().isLogin()) {
            binding.tvUserName.setText(UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getUser_name());
        } else {
            if (MmkvUtils.get(RequestInfoModel.LOGIN_OUT, false)) {
                binding.tvUserName.setText("登录/注册");
            } else {
                binding.tvUserName.setText("游客账号");
            }
        }

        if (UserInfoUtils.getInstance().isVip()) {
            binding.ivUserVipStatus.setImageResource(R.mipmap.icon_members_02);
            binding.stVipContainer.setBackgroundResource(R.mipmap.user_mine_membershipcard_02);
        } else {
            binding.ivUserVipStatus.setImageResource(R.mipmap.icon_members_01);
            binding.stVipContainer.setBackgroundResource(R.mipmap.user_mine_membershipcard_01);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_user_mine;
    }


    private void showCommTipDialog() {
        hideCommTipDialog();
        commonTipsDialog = new CommonTipsDialog(getContext(), "你确定要清除缓存吗？");
        commonTipsDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
            }
        });
        commonTipsDialog.setConfirmListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
                showLoadingDialog();
                binding.stClearCache.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        hideLoadingDialog();
                        ToastUtils.showShort("清理成功");
                    }
                }, 1500);
            }
        });
        commonTipsDialog.show();
    }

    private void hideCommTipDialog() {
        if (commonTipsDialog != null) {
            commonTipsDialog.dismiss();
            commonTipsDialog = null;
        }
    }
}